/*
 * test_net_s.cpp
 *
 *  Created on: May 19, 2014
 *      Author: killerg
 */

#include "share/net_s.h"
#include "share/clock.h"
#include "share/util.h"
#include <assert.h>
#include <vector>
using namespace std;

enum EStatus {
	Noexist = -1,
	Initing = 0,
	Running = 1,
	Shutting = 2,
};

static const char* NAME[] = {
		"Noexist", "Initing", "Running", "Shutting",
};



struct proc_t {
	string name;
	EStatus status;
	string refresh;
	string machine;
};

struct machine_t {
	string name;
	tran_s* tran;
};

static map<string, proc_t> proces;
static map<string, machine_t> machines;
static map<tran_s*, string> trans_machine;

bool test_center()
{
	my_clock_t clock;
	clock.init(5);
	net_s nets;
	nets.init(10000, NULL, 1000, true, 1000);


	bool ok = nets.start_accept(
			"0.0.0.0",
			9876,
			[](tran_s* tran){
				assert(trans_machine.count(tran) == 0);
				trans_machine[tran] = "";
			},
			[](tran_s* tran){
				auto machine_name = trans_machine[tran];
				if (machine_name != "") {
					machines[machine_name].tran = NULL;
				}
				trans_machine.erase(tran);
			});
	assert(ok);

	while (true) {
		clock.begin_update();

		for (auto &itr : trans_machine) {
			static char buffer[2048];
			if (itr.first->read_line(buffer) > 0) {
				vector<string> args;
				split(buffer, " ", args);

				if (itr.second == "") {
					if (machines.count(args[0]) == 0) {
						auto &machine = machines[args[0]];
						machine.tran = itr.first;
					} else {
						itr.first->disconnect(true);
					}
				} else {

				}

				if (args.size() == 0) {
					continue;
				}


			}
		}

		nets.update();
		clock.end_update();
		clock.wait_frame();
		if (clock.sec_jump) {
		}
	}

	nets.stop_accept();

	nets.destroy();
	clock.destroy();
	return true;
}
